package com.xiangyueEducation.uploaderCloud.controller;

import cn.hutool.core.bean.BeanUtil;
import com.xiangyueEducation.uploaderCloud.context.BaseContext;
import com.xiangyueEducation.uploaderCloud.pojo.dto.ArticleDTO;
import com.xiangyueEducation.uploaderCloud.pojo.dto.PageQuery;
import com.xiangyueEducation.uploaderCloud.pojo.entity.Article;
import com.xiangyueEducation.uploaderCloud.pojo.vo.PageVo;
import com.xiangyueEducation.uploaderCloud.service.ArticleService;
import com.xiangyueEducation.uploaderCloud.utils.Result;
import com.xiangyueEducation.uploaderCloud.utils.enums.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/article")
public class ArticleController {
    //该类下方法未测试
    @Autowired
    private ArticleService articleService;

    @PostMapping("/teacher")
    public Result addArticle(@RequestBody ArticleDTO articleDTO){
        Article article = BeanUtil.copyProperties(articleDTO, Article.class);
        article.setPublisherName(BaseContext.getCurrentUser().getRealName());
        article.setUserId(BaseContext.getCurrentUser().getId());
        articleService.save(article);
        Long id = article.getId();
        return Result.ok(id);
    }
    //分页获取文章标题，图片标题，分页条件查询，如果没有
    @GetMapping("/all/main")
    public Result queryMainArticle(PageQuery pageQuery,String keyword){
        Result result = pageQuery.checkPage();
        if (result!=null){
            return result;
        }
        PageVo pageVo = articleService.pageQuery(pageQuery,keyword);
        return Result.ok(pageVo);
    }
    //查询文章的细节
    @GetMapping("/all/{id}")
    public Result queryArticle(@PathVariable Long id){
        return Result.ok(articleService.getById(id));
    }
    //修改文章
    @PutMapping("/teacher")
    public Result updateArticle(@RequestBody ArticleDTO articleDTO){
        if (articleDTO.getId()==null)return Result.build(articleDTO,ResultCodeEnum.MUST_HAVE_ID);
        //属性拷贝
        Article article = BeanUtil.copyProperties(articleDTO, Article.class);
        article.setPublisherName(BaseContext.getCurrentUser().getRealName());
        article.setUserId(BaseContext.getCurrentUser().getId());
       //在xml文件中实现动态更新
        // 只能修改自己的文章
        if(!article.getUserId().equals(BaseContext.getCurrentUser().getId())){
            return Result.build(article,ResultCodeEnum.ONLY_CAN_UPDATE_YOURSELF);
        }
        // 不能改真实姓名
        articleService.update(article);
        return Result.ok(ResultCodeEnum.SUCCESS);
    }
    //删除文章
    @DeleteMapping("/teacher/{id}")
    public Result deleteArticle(@PathVariable Long id){
        // 只能删自己的
        Article article = articleService.getById(id);
        if (!article.getUserId().equals(BaseContext.getCurrentUser().getId())){
            return Result.build(article,ResultCodeEnum.ONLY_CAN_UPDATE_YOURSELF);
        }
        else{
            articleService.removeById(id);
        }
        return Result.ok(ResultCodeEnum.SUCCESS);
    }
}
